package com.jinlejia.erp.ui.common;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.BuildConfig;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivitySettingBinding;
import com.jinlejia.erp.utils.FileUtil;
import com.jinlejia.erp.utils.GlideCacheUtil;
import com.qiyukf.unicorn.api.Unicorn;
import com.utils.updateapp.UpdateUtils;
import com.weight.CustomToast;

import java.io.File;


/**
 * 设置Activity
 */

public class SettingActivity extends AppCompatActivity {

    //动态授权
    final int REQUEST_EXTERNAL_STORAGE = 1;
    String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    private ActivitySettingBinding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_setting);
        binding.setEvent(this);
        initView();
    }


    /**
     * 检查更新方法
     */
    public void checkupdate(){
        //检查授权
        int permission = ActivityCompat.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE");
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 没有写的权限，去申请写的权限，会弹出对话框
            ActivityCompat.requestPermissions( this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }
        UpdateUtils.getInstance(this).checkAndUpdateAndShowMessage(BuildConfig.VERSION_NAME, message -> CustomToast.showToast(SettingActivity.this, message, 2500));
    }


    /**
     * 初始化视图
     */
    private void initView() {

        long imageSize = GlideCacheUtil.getInstance().getCacheSize_number(SettingActivity.this);
        long attachSize = FileUtil.getFolderSize( new File(Environment.getExternalStorageDirectory(), "JljEnclosure") );
        binding.settingCheckText.setText( FileUtil.getFormatSize(imageSize+attachSize) );
        try {
            binding.settingCheckText.setText("当前版本: V" + getPackageManager().getPackageInfo("com.jinlejia.erp", 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn://返回
                finish();
                break;
            case R.id.setting_change_password://修改密码
                Intent intent = new Intent(SettingActivity.this,ChangePwdActivity.class);
                startActivity(intent);
                break;
            case R.id.setting_clear_cache://清除缓存
                GlideCacheUtil.getInstance().clearImageAllCache(SettingActivity.this);
                FileUtil.deleteFolderFile( new File(Environment.getExternalStorageDirectory(), "JljEnclosure").getAbsolutePath(),false );
                //获取大小
                long imageSize = GlideCacheUtil.getInstance().getCacheSize_number(SettingActivity.this);
                long attachSize = FileUtil.getFolderSize( new File(Environment.getExternalStorageDirectory(), "JljEnclosure") );
                Unicorn.clearCache();
                binding.settingCheckText.setText( FileUtil.getFormatSize(imageSize+attachSize) );
                break;
            case R.id.setting_check_update://检测更新
                checkupdate();
                break;
        }
    }
}
